#include <string>
#include <iostream>
#include "comm.hpp"
#include "Log.hpp"
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <cerrno>
#include <cstring>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "thread.hpp"
#include "InetAddr.hpp"

// 修改成读线程和写线程

void Usage(std::string proc)
{
    std::cout << "Usage: \n\t" << proc << " server_ip server_port" << std::endl;
}

class ThreadDate
{
public:
    ThreadDate(int sk, struct sockaddr_in &addr)
        : _sock(sk), _serveraddr(addr)
    {
    }

    ~ThreadDate()
    {
    }

public:
    int _sock;
    InerAddr _serveraddr;
};

//接收
void RecverRoutine(ThreadDate &td)
{
    char buffer[4096];
    while(true)
    {
        struct sockaddr_in tmp;
        socklen_t len=sizeof(tmp);
        int n=recvfrom(td._sock,buffer,sizeof(buffer)-1,0,(struct sockaddr *)&tmp,&len);
        if(n>0)
        {
            buffer[n]=0;
            std::cerr<<buffer<<std::endl;
        }
        else
            break;
    }
}

// 发送
void SenderRoutine(ThreadDate &td)
{
    while (true)
    {
        std::string inbuffer;
        std::cout << "Plead Enter# ";
        std::getline(std::cin, inbuffer);
        
        struct sockaddr_in server=td._serveraddr.GetAddr();
        ssize_t n = sendto(td._sock, inbuffer.c_str(), sizeof(inbuffer), 0, (struct sockaddr *)&server, sizeof(server));
        if(n<=0)
            std::cerr<<"send errno"<<std::endl;
    }
}

int main(int args, char *argv[])
{
    if (args != 3)
    {
        Usage(argv[0]);
        exit(Usage_err);
    }
    std::string server_ip = argv[1];
    uint16_t server_port = std::stoi(argv[2]);

    // 创建套接字
    int sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock < 0)
    {
        std::cerr << "socket error: " << strerror(errno) << std::endl;
        exit(Socket_error);
    }
    std::cout << "create socket success:" << sock << std::endl;

    // 不需要bind，由OS随机
    // 填充server信息
    struct sockaddr_in server;
    bzero(&server, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(server_port);
    server.sin_addr.s_addr = inet_addr(server_ip.c_str());

    ThreadDate td(sock, server);
    Thread<ThreadDate> recver("recsver", RecverRoutine, td);
    Thread<ThreadDate> sender("sender", SenderRoutine, td);

    recver.Start();
    sender.Start();

    recver.Join();
    sender.Join();
    close(sock);

    // while(true)
    // {
    //     std::string inbuffer;
    //     std::cout<<"Plead Enter# ";
    //     std::getline(std::cin,inbuffer);
    //     ssize_t n=sendto(sock,inbuffer.c_str(),sizeof(inbuffer),0,(struct sockaddr*)&server,sizeof(server));

    //     //发送成功
    //     if(n>0)
    //     {
    //         //收消息
    //         char buff[1024];
    //         struct sockaddr_in tmp;
    //         socklen_t len=sizeof(tmp);
    //         int m=recvfrom(sock,buff,sizeof(buff)-1,0,(struct sockaddr*)&tmp,&len);
    //         if(m>0)
    //         {
    //             buff[1024]=0;
    //             std::cout<<"server echo# "<<buff<<std::endl;
    //         }
    //         else
    //             break;
    //     }
    //     else
    //         break;
    // }
    // close (sock);
    return 0;
}